//
//  AddAddressVC.m
//  MyNewProject
//
//  Created by sun on 2019/2/2.
//  Copyright © 2019 sun. All rights reserved.
//

#import "AddAddressVC.h"
#import "JYAddressPicker.h"
@interface AddAddressVC ()<UITextFieldDelegate>
@property(nonatomic , retain)NSMutableDictionary * xm_PostDictionary;
@end

@implementation AddAddressVC
-(NSMutableDictionary *)xm_PostDictionary{
    NSString *uid = self.uid;
    NSString *loginKey = self.loginKey;
    if (!_xm_PostDictionary) {
        if (self.isEdit) {
             _xm_PostDictionary = [[NSMutableDictionary alloc]initWithDictionary:@{@"uid":uid,@"loginKey":loginKey,@"id":self.addressModel.addrId,@"name":self.addressModel.name,@"tel":self.addressModel.tel,@"city":self.addressModel.city,@"addr":self.addressModel.addr}];
        }else{
            _xm_PostDictionary = [[NSMutableDictionary alloc]initWithDictionary:@{@"uid":uid,@"loginKey":loginKey,@"name":@"",@"tel":@"",@"city":@"",@"addr":@""}];
        }
    }
    return _xm_PostDictionary;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.gk_navTitle = @"新增地址";
    self.view.backgroundColor = SKRGBColor(245, 245, 245);
    if (self.isEdit) {
        UIButton  * customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [customBtn setTitle:@"删除" forState:UIControlStateNormal];
        [customBtn setTitleColor:SKColorWithHex(0xFF2424) forState:UIControlStateNormal];
        customBtn.titleLabel.font = SKFONT_Medium(13);
        [customBtn addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.gk_navRightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customBtn];
    }
    
    [self initView];
    
}
-(void)deleteBtn:(UIButton*)sender
{
    NSString *uid = self.uid;
    NSString *loginKey = self.loginKey;
    [YQNetworking postWithUrl:@"My/addrDel" params:@{@"uid":uid,@"loginKey":loginKey,@"id":self.addressModel.addrId} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        if (self.xm_block) {//回调刷新
            self.xm_block();
        }
        [self.view makeToast:@"地址已删除" duration:2 position:CSToastPositionCenter];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failBlock:^(NSError *error) {
        
    }];
}
#pragma mark -- 没收收货地址的界面
-(void)initView
{
    NSArray * leftNameArr = @[@"收货人",@"联系电话", @"邮政编码", @"所在地区",@"详细地址"];
    NSArray * placeHoldArr = @[@"请填写收货人姓名",@"请输入收货人的联系方式", @"请输入邮政编码", @"请选择所在地区",@"请输入详细地址"];
    CGFloat bgViewHeight = 60;
    for (int i = 0; i < 5; i++) {
        UIView * bgView =[[UIView alloc] initWithFrame:CGRectMake(0, SKTopBar_Height + 20 + bgViewHeight*i, SCREEN_WIDTH, bgViewHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.tag = 10000000 + i;
        [self.view addSubview:bgView];
        
        UILabel * label = [[UILabel alloc] init];
        label.textColor = SKColorWithHex(0x444444);
        label.font = SKFONT_Medium(15.0);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = leftNameArr[i];
        [bgView addSubview:label];
        CGFloat labWidth = [NSString getWidthWithString:@"联系电话" font:label.font lineBreakModel:NSLineBreakByCharWrapping];
        label.frame = CGRectMake(15, 0, labWidth, bgViewHeight);
        UITextField * textField =[[UITextField alloc] initWithFrame:CGRectMake(labWidth+15 +20, 0, SCREEN_WIDTH- 15 - labWidth -20 -15, bgViewHeight)];
        textField.textColor = SKColorWithHex(0x444444);
        if (self.isEdit) {
            switch (i) {
                case 0:{
                    textField.text = self.addressModel.name;
                }
                    break;
                case 1:{
                    textField.text = self.addressModel.tel;
                }
                    break;
                case 2:{
                    textField.text = self.addressModel.abbreviation;
                }
                    break;
                case 3:{
                    textField.text = self.addressModel.city;
                }
                case 4: {
                    textField.text = self.addressModel.addr;
                }
                    break;
                default:
                    break;
            }
        }
        textField.font = SKFONT_Medium(15.0);
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeHoldArr[i] attributes:@{NSForegroundColorAttributeName:SKColorWithHex(0x999999), NSFontAttributeName:textField.font}];
        textField.attributedPlaceholder = attrString;
        textField.delegate = self;
        textField.tag = 20000000 + i;
        [bgView addSubview:textField];
        
        UIImageView * imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, label.bottom -0.5, SCREEN_WIDTH, 0.5)];
        imageView.backgroundColor =SKRGBColor(237, 237, 237);
        [bgView addSubview:imageView];
        if (i == 3) {
            UIImageView * arrowImageView =[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -15 -6, (bgViewHeight-11)/2, 6, 11)];
            textField.userInteractionEnabled = NO;
            arrowImageView.image  = SKIMAGE_NAMED(@"rightArrow");
            [bgView addSubview:arrowImageView];
            textField.frame = CGRectMake(labWidth+15 +20, 0, SCREEN_WIDTH- 15 - labWidth -20 -15 -20-10, bgViewHeight);
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = bgView.bounds;
            [button addTarget:self action:@selector(cityDeal:) forControlEvents:UIControlEventTouchUpInside];
            [bgView  addSubview:button];
        }
    }
    UIFont * font = [UIFont systemFontOfSize:18];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, SCREENH_HEIGHT - 150 , SCREEN_WIDTH -20 *2 , 50);
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    button.backgroundColor = SKColorWithHex(0xE33030);
    [button setTitle:@"保存" forState:UIControlStateNormal];
    button.titleLabel.font = font ;
    button.enabled = false;
    button.tag = 520;
    [button setTitleColor:SKColorWithHex(0xFFFFFF) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(confirmDeal:) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:button];
}
#pragma mark -- 增加收货地址
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UITextField *textField0 = [self.view viewWithTag:20000000];
    UITextField *textField1 = [self.view viewWithTag:20000001];
    UITextField *textField2 = [self.view viewWithTag:20000002];
    UITextField *textField3 = [self.view viewWithTag:20000003];
    UITextField *textField4 = [self.view viewWithTag:20000004];
    BOOL isNotNull0 = textField0.text.length > 0;
    BOOL isNotNull1 = textField1.text.length > 0;
    BOOL isNotNull2 = textField2.text.length > 0;
    BOOL isNotNull3 = textField3.text.length > 0;
    BOOL isNotNull4 = textField4.text.length > 0;
    UIButton *btn = [self.view viewWithTag:520];
    if (isNotNull0 && isNotNull1 && isNotNull2 && isNotNull3 && isNotNull4) {
        btn.enabled = true;
    }else {
        btn.enabled = false;
    }
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{//结束编辑 塞数据
    switch (textField.tag - 20000000) {
        case 0:{//联系人
            [self.xm_PostDictionary setObject:textField.text forKey:@"name"];
        }
            break;
        case 1:{//联系方式
            [self.xm_PostDictionary setObject:textField.text forKey:@"tel"];
        }
            break;
        case 4:{//详细地址
            [self.xm_PostDictionary setObject:textField.text forKey:@"addr"];
        }
            break;
        default:
            break;
    }
}
-(void)confirmDeal:(UIButton *)sender
{
    [self.view endEditing:YES];
    if ([self.xm_PostDictionary[@"name"] isEqualToString:@""]) {
        [self.view makeToast:@"请填写收货人姓名" duration:2 position:CSToastPositionCenter];
        return;
    }
    if ([self.xm_PostDictionary[@"tel"] isEqualToString:@""]) {
        [self.view makeToast:@"请填写收货人联系方式" duration:2 position:CSToastPositionCenter];
        return;
    }
    if ([self.xm_PostDictionary[@"city"] isEqualToString:@""]) {
        [self.view makeToast:@"请填写收货人所在地" duration:2 position:CSToastPositionCenter];
        return;
    }
    if ([self.xm_PostDictionary[@"addr"] isEqualToString:@""]) {
        [self.view makeToast:@"请填写收货人详细地址" duration:2 position:CSToastPositionCenter];
        return;
    }
    [YQNetworking postWithUrl:self.isEdit?@"My/addrEdit":@"My/addrAdd" params:self.xm_PostDictionary reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        if (self.xm_block) {//回调刷新
            self.xm_block();
        }
        [self.view makeToast:self.isEdit?@"修改成功！":@"添加成功！" duration:2 position:CSToastPositionCenter];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failBlock:^(NSError *error) {
        
    }];
}
-(void)cityDeal:(UIButton*)sender
{
    SKWeakSelf;
    JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self];
    addressPicker.selectedItemBlock = ^(NSArray *addressArr) {
        
        NSString *province = [addressArr objectAtIndex:0][@"text"];
        NSString *city = [addressArr objectAtIndex:1][@"text"];
        NSString *county = [addressArr objectAtIndex:2][@"text"];
        
        SKLog(@"%@",[NSString stringWithFormat:@"%@%@%@",province,city,county]);
        
        UIView * bgView = [weakSelf.view viewWithTag:10000000 +3];
        UITextField * textField = [bgView viewWithTag:20000000 +3];
        textField.text = [NSString stringWithFormat:@"%@%@%@",province,city,county];
        [self.xm_PostDictionary setObject:textField.text forKey:@"city"];
    };
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
